package jannex.internal;

import jannex.JannexException;
import jannex.label.LabelMap;
import jannex.sql.JxResultSet;
import jannex.sql.JxResultSetMetaData;

import java.util.Map;

public class ResultSubSetMetaData implements JxResultSetMetaData {

    private final JxResultSetMetaData metaData;
    private final int[] columns;
    private final LabelMap<Integer> labelIndex;


    public ResultSubSetMetaData(JxResultSetMetaData metaData, int[] columns, LabelMap<Integer> labelIndex) {
        this.metaData = metaData;
        this.columns = columns;
        this.labelIndex = labelIndex;
    }

    @Override
    public boolean isAutoIncrement(String columnLabel) throws JannexException {
        return metaData.isAutoIncrement(columnLabel);
    }

    @Override
    public boolean isSearchable(String columnLabel) throws JannexException {
        return metaData.isSearchable(getColumn(columnLabel));
    }

    @Override
    public boolean isCurrency(String columnLabel) throws JannexException {
        return metaData.isCurrency(getColumn(columnLabel));
    }

    @Override
    public int isNullable(String columnLabel) throws JannexException {
        return metaData.isNullable(getColumn(columnLabel));
    }

    @Override
    public boolean isSigned(String columnLabel) throws JannexException {
        return metaData.isSigned(getColumn(columnLabel));
    }

    @Override
    public int getColumnDisplaySize(String columnLabel) throws JannexException {
        return metaData.getColumnDisplaySize(getColumn(columnLabel));
    }

    @Override
    public String getColumnName(String columnLabel) throws JannexException {
        return metaData.getColumnName(getColumn(columnLabel));
    }

    @Override
    public String getSchemaName(String columnLabel) throws JannexException {
        return metaData.getSchemaName(getColumn(columnLabel));
    }

    @Override
    public int getPrecision(String columnLabel) throws JannexException {
        return metaData.getPrecision(getColumn(columnLabel));
    }

    @Override
    public int getScale(String columnLabel) throws JannexException {
        return metaData.getScale(getColumn(columnLabel));
    }

    @Override
    public String getTableName(String columnLabel) throws JannexException {
        return metaData.getTableName(getColumn(columnLabel));
    }

    @Override
    public String getCatalogName(String columnLabel) throws JannexException {
        return metaData.getCatalogName(getColumn(columnLabel));
    }

    @Override
    public int getColumnType(String columnLabel) throws JannexException {
        return metaData.getColumnType(getColumn(columnLabel));
    }

    @Override
    public String getColumnTypeName(String columnLabel) throws JannexException {
        return metaData.getColumnTypeName(getColumn(columnLabel));
    }

    @Override
    public boolean isReadOnly(String columnLabel) throws JannexException {
        return metaData.isReadOnly(getColumn(columnLabel));
    }

    @Override
    public boolean isWritable(String columnLabel) throws JannexException {
        return metaData.isWritable(getColumn(columnLabel));
    }

    @Override
    public boolean isDefinitelyWritable(String columnLabel) throws JannexException {
        return metaData.isDefinitelyWritable(getColumn(columnLabel));
    }

    @Override
    public String getColumnClassName(String columnLabel) throws JannexException {
        return metaData.getColumnClassName(getColumn(columnLabel));
    }

    @Override
    public int getColumn(String columnLabel) throws JannexException {
        Integer ix = labelIndex.get(columnLabel);
        if (ix == null) {
            return -1;
        }
        return ix;
    }

    @Override
    public boolean containsColumnLabel(String columnLabel) throws JannexException {
        return labelIndex.containsKey(columnLabel);
    }

    @Override
    public String[] getColumnLabels() throws JannexException {
        return labelIndex.keySet().toArray();
    }

    @Override
    public Map<String, Integer> getColumnLabelMap() throws JannexException {
        return labelIndex;
    }

    @Override
    public int getColumnCount() throws JannexException {
        return labelIndex.size();
    }

    @Override
    public boolean isAutoIncrement(int columnIndex) throws JannexException {
        return metaData.isAutoIncrement(columns[columnIndex-1]);
    }

    @Override
    public boolean isCaseSensitive(int columnIndex) throws JannexException {
        return metaData.isCaseSensitive(columns[columnIndex-1]);
    }

    @Override
    public boolean isSearchable(int columnIndex) throws JannexException {
        return metaData.isSearchable(columns[columnIndex-1]);
    }

    @Override
    public boolean isCurrency(int columnIndex) throws JannexException {
        return metaData.isCurrency(columns[columnIndex-1]);
    }

    @Override
    public int isNullable(int columnIndex) throws JannexException {
        return metaData.isNullable(columns[columnIndex-1]);
    }

    @Override
    public boolean isSigned(int columnIndex) throws JannexException {
        return metaData.isSigned(columns[columnIndex-1]);
    }

    @Override
    public int getColumnDisplaySize(int columnIndex) throws JannexException {
        return metaData.getColumnDisplaySize(columns[columnIndex-1]);
    }

    @Override
    public String getColumnLabel(int columnIndex) throws JannexException {
        return metaData.getColumnLabel(columns[columnIndex-1]);
    }

    @Override
    public String getColumnName(int columnIndex) throws JannexException {
        return metaData.getColumnName(columns[columnIndex-1]);
    }

    @Override
    public String getSchemaName(int columnIndex) throws JannexException {
        return metaData.getSchemaName(columns[columnIndex-1]);
    }

    @Override
    public int getPrecision(int columnIndex) throws JannexException {
        return metaData.getPrecision(columns[columnIndex-1]);
    }

    @Override
    public int getScale(int columnIndex) throws JannexException {
        return metaData.getScale(columns[columnIndex-1]);
    }

    @Override
    public String getTableName(int columnIndex) throws JannexException {
        return metaData.getTableName(columns[columnIndex-1]);
    }

    @Override
    public String getCatalogName(int columnIndex) throws JannexException {
        return metaData.getCatalogName(columns[columnIndex-1]);
    }

    @Override
    public int getColumnType(int columnIndex) throws JannexException {
        return metaData.getColumnType(columns[columnIndex-1]);
    }

    @Override
    public String getColumnTypeName(int columnIndex) throws JannexException {
        return metaData.getColumnTypeName(columns[columnIndex-1]);
    }

    @Override
    public boolean isReadOnly(int columnIndex) throws JannexException {
        return metaData.isReadOnly(columns[columnIndex-1]);
    }

    @Override
    public boolean isWritable(int columnIndex) throws JannexException {
        return metaData.isWritable(columns[columnIndex-1]);
    }

    @Override
    public boolean isDefinitelyWritable(int columnIndex) throws JannexException {
        return metaData.isDefinitelyWritable(columns[columnIndex-1]);
    }

    @Override
    public String getColumnClassName(int columnIndex) throws JannexException {
        return metaData.getColumnClassName(columns[columnIndex-1]);
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) {
        return iface.equals(JxResultSetMetaData.class) || metaData.isWrapperFor(iface);
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T> T unwrap(Class<T> iface) {
        if (iface.equals(JxResultSetMetaData.class)) {
            return (T) metaData;
        }
        return metaData.unwrap(iface);
    }
}
